import javax.crypto.*;
import java.io.*;

public class Start
{
	public static void main(String[] args)
	{
		try
		{
			byte[] cipher;
			byte[] text;
				
			String getInputString = "Enter the sequence to be encrypted\n";
			String getIVString = "Enter IV: ";
			String getKeyString = "Enter key: ";
				
			System.out.println("CNT5410 Project Part 1");   
			System.out.println("Please select encryption method:");
			System.out.println("1. 3DES/CFB");
			System.out.println("2. 3DES/CBC");
			System.out.println("3. 3DES/PCBC");
			System.out.println("4. RC4");
			System.out.println("5. RSA");
			
			System.out.print("Enter selection: ");
			String opt = System.console().readLine();
                        
			//do 3DES/CFB
			if (opt.equals("1"))
			{
				System.out.println("Encryption Type: 3DES/CFB");  
				
				String key = Utils.GetUserInput(getKeyString);
				byte[] IV = Utils.GetIV(Utils.GetUserInput(getIVString));
				byte[] input = Utils.GetUserInput(getInputString).getBytes(); 
				int k = Integer.parseInt(Utils.GetUserInput("Enter k size (8/16/32/64): ")); 
				
				CFB cfb = new CFB();
				
				if (k == 8 || k == 16 || k == 32 || k == 64)
					cfb.k = k/8;
				
				cipher = cfb.Encrypt(cfb.GetKeyFromString(key), IV, input);
				System.out.println("Encrypted Text: " + Utils.ToHexString(cipher));
				
				text = cfb.Decrypt(cfb.GetKeyFromString(key), IV, cipher);
				System.out.println("Decrypted Text: " + Utils.ByteToString(text));
			}
			
			//do 3DES/CBC
			else if (opt.equals("2"))
			{
				System.out.println("Encryption Type: 3DES/CBC");  
				
				String key = Utils.GetUserInput(getKeyString);
				byte[] IV = Utils.GetIV(Utils.GetUserInput(getIVString));
				byte[] input1 = Utils.GetUserInput("Enter first sequence: ").getBytes(); 
				byte[] input2 = Utils.GetUserInput("Enter second sequence: ").getBytes(); 

				CBC cbc = new CBC();
				
				cipher = cbc.GetMac(cbc.GetKeyFromString(key), IV, input1);
				byte[] cipher2 = cbc.GetMac(cbc.GetKeyFromString(key), IV, input2);
				System.out.println("MAC for sequence 1: " + Utils.ToHexString(cipher));
				System.out.println("MAC for sequence 2: " + Utils.ToHexString(cipher2));
				
				boolean match = Utils.ToHexString(cipher).equals(Utils.ToHexString(cipher2));
				if (match)
					System.out.println("The MACs are equal");
				else
					System.out.println("The MACs are NOT equal");
			}
			
			//do 3DES/PCBC
			else if (opt.equals("3"))
			{
				System.out.println("Encryption Type: 3DES/PCBC");  
				
				String key = Utils.GetUserInput(getKeyString);
				byte[] IV = Utils.GetIV(Utils.GetUserInput(getIVString));
				byte[] input = Utils.GetUserInput(getInputString).getBytes(); 

				PCBC pcbc = new PCBC();
			
				cipher = pcbc.Encrypt(pcbc.GetKeyFromString(key), IV, input);
				System.out.println("Encrypted Text: " + Utils.ToHexString(cipher));
				
				String i = Utils.GetUserInput("Do you want to modify cipher text? (Y/N)");
				if (i.toLowerCase().equals("y"))
				{
					cipher[0] = (byte)(cipher[0] ^ 0xFF);
					System.out.println("Modified Encrypted Text: " + Utils.ToHexString(cipher));
				}
					
				boolean[] b = new boolean[1];
				text = pcbc.Decrypt(pcbc.GetKeyFromString(key), IV, cipher, b);
				System.out.println("Decrypted Text: " + Utils.ByteToString(text));
				
				if (b[0])
					System.out.println("Cipher text was NOT modified");
				else
					System.out.println("Cipher text was modified");
			}
			//do RC4
			else if (opt.equals("4"))
			{
				String input = Utils.GetUserInput(getInputString);
			
			    byte[] byteArray = input.getBytes(); 
				System.out.println("clearText: " + input);
				RC4 rc4 = new RC4(byteArray);
                            
			}
			
			//do RSA
			else if (opt.equals("5"))
			{
				String input = Utils.GetUserInput(getInputString);
			
				byte[] byteArray = input.getBytes(); 
                                System.out.println("clearText: " + input);
				RsaEncryption modeOne = new RsaEncryption(13, 19, 5, byteArray);
				
				RsaEncryption modeTwo = new RsaEncryption(11, 23, 3, byteArray);
			}
			
			else 
				System.out.println("Entered value not valid.");
		}
		catch (Exception e) { e.printStackTrace(); } 
	}
}













